﻿using AnesSystem.Models;
using AnesSystem.Services;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;
using Excel = Microsoft.Office.Interop.Excel;

namespace AnesSystem.Util
{

    /// <summary>
    /// 导出Excel类
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <typeparam name="U"></typeparam>
    public class QualityWorkExport
    {
        private static QualityWorkExport instance = null;

        private static Dictionary<int, Dictionary<int, string>> rowMapColData = new Dictionary<int, Dictionary<int, string>>();

        public static QualityWorkExport getInstance()
        {
            if (null == instance)
            {
                instance = new QualityWorkExport();
            }
            return instance;
        }

        public static Dictionary<int, Dictionary<int, string>> CalData(DateTime startTime, DateTime endTime, string accountName)
        {
            rowMapColData.Clear();
            if (null == startTime || null == endTime) return rowMapColData;
            List<PatientLinkModel> patientLinks = PatientLinkService.Instance.GetFinishOperByTime(startTime, endTime);
            patientLinks = patientLinks.OrderBy(u => u.Status.InRoomTime).ToList();
            List<AccountModel> selAccountList = AccountService.Instance.findAccountListByName(string.IsNullOrEmpty(accountName)?"": accountName);
            List<string> accountIdList = new List<string>();

            if (null != selAccountList && selAccountList.Count > 0)
            {
                for (int i = 0; i < selAccountList.Count; i++)
                {
                    accountIdList.Add(selAccountList[i].Id.ToString());
                }
            }

            List<PatientLinkModel> patientLinkList = new List<PatientLinkModel>();
            if (null != accountIdList && accountIdList.Count > 0)
            {
                foreach (PatientLinkModel patientLink in patientLinks)
                {
                    for (int j = 0; j < accountIdList.Count; j++)
                    {
                        if (ExportToExcel.isOperOfAllDoctor(patientLink, accountIdList[j]))
                        {
                            patientLinkList.Add(patientLink);
                        }
                    }
                }
            }
            else
            {
                patientLinkList.AddRange(patientLinks);
            }
            if (accountName != null && !accountName.Equals(""))
            {
                foreach (PatientLinkModel oper in patientLinks)
                {
                    if (accountName.Equals(oper.Patient.Brxm))
                    {
                        patientLinkList.Add(oper);
                    }
                }
            }

            if (null == patientLinkList || patientLinkList.Count == 0) return rowMapColData;

            int row = 0;
            for (int i = 0; i < patientLinkList.Count; i++)
            {
                //System.out.println("现在是第" + i);
                Dictionary<int, string> colMapData = new Dictionary<int, string>();
                PatientLinkModel patientLink = patientLinkList[i];
                string patName = patientLink.Patient.Brxm == null ? "" : patientLink.Patient.Brxm;
                if (patName.Length == 0) continue;

                DateTime inRoomTime = patientLink.Status.InRoomTime;
                string inRoomTimeStr = null == inRoomTime ? "" : inRoomTime.ToString("yyyy-MM-dd");
                colMapData.Add(0, inRoomTimeStr);

                //病人姓名
                colMapData.Add(1, patName);

                //病人性别
                string patSex = patientLink.Patient.Xb == null ? "" : patientLink.Patient.Xb;
                colMapData.Add(2, patSex);

                colMapData.Add(3, patientLink.Patient.Nl);

                //床号
                string deptName = patientLink.Patient.Ksmc == null ? "" : patientLink.Patient.Ksmc;
                string bed = patientLink.Patient.Zych == null ? "" : patientLink.Patient.Zych;
                colMapData.Add(4, deptName + "-" + bed);

                //住院号
                string zyh = patientLink.Patient.Zyh == null ? "" : patientLink.Patient.Zyh;
                colMapData.Add(5, zyh);

                //拟施手术
                string operName = getYiShiOper(patientLink);
                if (operName.Length == 0) operName = getNiShiOper(patientLink);
                colMapData.Add(6, operName);

                //ASA分级
                string asaLevel = patientLink.AnesthesiaRecord.Asa.Level == null ? "" : patientLink.AnesthesiaRecord.Asa.Level;
                colMapData.Add(7, asaLevel);

                //急诊
                if (patientLink.AnesthesiaRecord.Asa.Jz)
                {
                    colMapData.Add(8, "1");
                }

                //麻醉方式
                string complicatedMethod = patientLink.AnesthesiaRecord.Mzff;
                if (complicatedMethod.Contains("气管插管全麻"))
                {
                    colMapData.Add(9, "1");
                }
                if (complicatedMethod.Contains("异型管全麻"))
                {
                    colMapData.Add(13, "1");
                }
                if (complicatedMethod.Contains("硬膜外"))
                {
                    colMapData.Add(14, "1");
                }
                if (complicatedMethod.Contains("腰麻"))
                {
                    colMapData.Add(15, "1");
                }
                if (complicatedMethod.Contains("腰硬联合"))
                {
                    colMapData.Add(16, "1");
                }
                if (complicatedMethod.Contains("神经阻滞"))
                {
                    colMapData.Add(17, "1");
                }
                if (complicatedMethod.Equals("静脉麻醉"))
                {
                    colMapData.Add(18, "1");
                }
                if (complicatedMethod.Contains("局麻"))
                {
                    colMapData.Add(19, "1");
                }
                if (complicatedMethod.Contains("复合麻醉"))
                {
                    colMapData.Add(20, "1");
                }

                //血管穿刺
                if (patientLink.AnesthesiaRecord.Zxjmcc)
                {
                    colMapData.Add(21, "1");
                }
                if (patientLink.AnesthesiaRecord.Dmcc)
                {
                    colMapData.Add(22, "1");
                }
                //镇痛泵
                if (patientLink.AnesthesiaRecord.PCIA)
                {
                    colMapData.Add(23, "√");
                }
                if (patientLink.AnesthesiaRecord.PCEA)
                {
                    colMapData.Add(24, "√");
                }

                //病人去向
                string quXiang = patientLink.AnesthesiaRecord.Brqw;
                if (null != quXiang)
                {
                    if (quXiang.Equals("复苏室") || quXiang.Equals("PACU"))
                    {
                        colMapData.Add(26, "√");
                    }
                    else if (quXiang.Equals("病房"))
                    {
                        colMapData.Add(27, "√");
                    }
                    else if (quXiang.Equals("ICU"))
                    {
                        colMapData.Add(28, "√");
                    }
                }
                QualityPojoModel qualityPojo = patientLink.QualityPojo;
                //质控指标
                if (qualityPojo.Zdss)
                {
                    colMapData.Add(29, "√");
                }
                if (qualityPojo.Fjhecss)
                {
                    colMapData.Add(30, "√");
                }
                if (qualityPojo.Fjhecqgbg)
                {
                    colMapData.Add(31, "√");
                }
                if (qualityPojo.Fjhgbmzfs)
                {
                    colMapData.Add(32, "√");
                }
                if (qualityPojo.Fjhecmz)
                {
                    colMapData.Add(33, "√");
                }
                if (qualityPojo.Szsxsyfy)
                {
                    colMapData.Add(34, "√");
                }
                if (qualityPojo.Wyqysza)
                {
                    colMapData.Add(35, "√");
                }
                if (qualityPojo.Ybhhdzdjd)
                {
                    colMapData.Add(36, "√");
                }
                if (false)
                {
                    colMapData.Add(37, "√");
                }
                if (false)
                {
                    colMapData.Add(38, "√");
                }

                //麻醉医生
                string mazuiDoc = patientLink.AnesthesiaRecord.Zmys == null ? "" : DataManager.Instance.GetAccountNameByID(patientLink.AnesthesiaRecord.Zmys);
                string mazuiDoc2 = patientLink.AnesthesiaRecord.Fmys1 == null ? "" : DataManager.Instance.GetAccountNameByID(patientLink.AnesthesiaRecord.Fmys1);
                string mazuiDoc3 = patientLink.AnesthesiaRecord.Fmys2 == null ? "" : DataManager.Instance.GetAccountNameByID(patientLink.AnesthesiaRecord.Fmys2);
                mazuiDoc = mazuiDoc + " " + mazuiDoc2 + " " + mazuiDoc3;
                colMapData.Add(39, mazuiDoc.Trim());

                //            //入手术室时间
                if (patientLink.Status.InRoomTime == null)
                {
                    colMapData.Add(41, "无");
                }
                else
                {
                    colMapData.Add(41, patientLink.Status.InRoomTime.ToString("yyyy-MM-dd HH:mm"));
                }

                //出手术室时间
                if (patientLink.Status.OutRoomTime == null)
                {
                    colMapData.Add(42, "无");
                }
                else
                {

                    colMapData.Add(42, patientLink.Status.OutRoomTime.ToString("yyyy-MM-dd HH:mm"));
                }

                //入室时长
                long totalTime;
                if (patientLink.Status.OutRoomTime != null && patientLink.Status.InRoomTime != null)
                {
                    totalTime = getTimeSpan(patientLink.Status.InRoomTime, patientLink.Status.OutRoomTime);                    
                    //totalTime = (long)(patientLink.Status.OutRoomTime - patientLink.Status.InRoomTime).TotalSeconds / 1000 / 60;//分钟
                }
                else
                {
                    totalTime = 0;
                }
                colMapData.Add(43, totalTime.ToString());

                //手术开始时间
                if (patientLink.Status.OperStartTime == null)
                {
                    colMapData.Add(44, "无");
                }
                else
                {
                    colMapData.Add(44, patientLink.Status.OperStartTime.ToString("yyyy-MM-dd HH:mm"));
                }
                //手术结束时间
                if (patientLink.Status.OperEndTime == null)
                {
                    colMapData.Add(45, "无");
                }
                else
                {
                    colMapData.Add(45, patientLink.Status.OperEndTime.ToString("yyyy-MM-dd HH:mm"));
                }
                //            手术时长
                long operationTime = patientLink.Status.OperSpanTime;//分钟
                //long minites = operationTime % 60;
                //long hour = operationTime / 60;
                //string operTime = hour > 0 ? hour + "小时" + minites + "分钟" : minites + "分钟";
                colMapData.Add(46, operationTime.ToString());

                rowMapColData.Add(row, colMapData);
                row++;
            }

            return rowMapColData;
        }

        public static string getYiShiOper(PatientLinkModel patientLink)
        {
            string nishiOper = "";
            if (null != patientLink.AnesthesiaRecord.Ysss1 && !"".Equals(patientLink.AnesthesiaRecord.Ysss1) &&
                    null != patientLink.AnesthesiaRecord.Ysss2 && !"".Equals(patientLink.AnesthesiaRecord.Ysss2))
                nishiOper += ("①" + patientLink.AnesthesiaRecord.Ysss1);
            else if (null != patientLink.AnesthesiaRecord.Ysss1 && !"".Equals(patientLink.AnesthesiaRecord.Ysss1) &&
                    null == patientLink.AnesthesiaRecord.Ysss2 || "".Equals(patientLink.AnesthesiaRecord.Ysss2))
            {
                nishiOper += (patientLink.AnesthesiaRecord.Ysss1);
            }
            else return "";
            if (null != patientLink.AnesthesiaRecord.Ysss2 && !"".Equals(patientLink.AnesthesiaRecord.Ysss2))
                nishiOper += (" ②" + patientLink.AnesthesiaRecord.Ysss2);
            if (null != patientLink.AnesthesiaRecord.Ysss3 && !"".Equals(patientLink.AnesthesiaRecord.Ysss3))
                nishiOper += (" ③" + patientLink.AnesthesiaRecord.Ysss3);
            if (null != patientLink.AnesthesiaRecord.Ysss4 && !"".Equals(patientLink.AnesthesiaRecord.Ysss4))
                nishiOper += (" ④" + patientLink.AnesthesiaRecord.Ysss4);
            return nishiOper;
        }

        public static string getNiShiOper(PatientLinkModel patientLink)
        {
            string nishiOper = "";
            if (null != patientLink.AnesthesiaRecord.Nsss1 && !"".Equals(patientLink.AnesthesiaRecord.Nsss1) &&
                    null != patientLink.AnesthesiaRecord.Nsss2 && !"".Equals(patientLink.AnesthesiaRecord.Nsss2))
                nishiOper += ("①" + patientLink.AnesthesiaRecord.Nsss1);
            else if (null != patientLink.AnesthesiaRecord.Nsss1 && !"".Equals(patientLink.AnesthesiaRecord.Nsss1) &&
                    null == patientLink.AnesthesiaRecord.Nsss2 || "".Equals(patientLink.AnesthesiaRecord.Nsss2))
            {
                nishiOper += (patientLink.AnesthesiaRecord.Nsss1);
            }
            else return "";
            if (null != patientLink.AnesthesiaRecord.Nsss2 && !"".Equals(patientLink.AnesthesiaRecord.Nsss2))
                nishiOper += (" ②" + patientLink.AnesthesiaRecord.Nsss2);
            if (null != patientLink.AnesthesiaRecord.Nsss3 && !"".Equals(patientLink.AnesthesiaRecord.Nsss3))
                nishiOper += (" ③" + patientLink.AnesthesiaRecord.Nsss3);
            if (null != patientLink.AnesthesiaRecord.Nsss4 && !"".Equals(patientLink.AnesthesiaRecord.Nsss4))
                nishiOper += (" ④" + patientLink.AnesthesiaRecord.Nsss4);
            return nishiOper;
        }
        public static long getTimeSpan(DateTime d1, DateTime d2)
        {
            long spanTime = 0;
            TimeSpan span = d2 - d1;
            long Minutes = span.Days * 24 * 60 + span.Hours * 60 + span.Minutes;
            if (Minutes > 0)
            {
                spanTime = Minutes;
            }
            return spanTime;
        }
    }

}
